package menu
{
	import flash.utils.Dictionary;
	import menu.model.MenuItemVO;
	import menu.model.MenuVO;
	import org.airmvc.Controller;
	import utils.T;
	import vo.CastleVO;
	import vo.GeneralVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-03-15
	 */
	public class MenuC extends Controller
	{
		
		public function MenuC()
		{
			super();
		}
		
		override protected function addListeners():void
		{
			interest(Nws.STRATEGY_ACTIVE_CASTLE, handleActiveCastle);
			interest(Nws.MENU_VERIFY_PARAM, verifyParam);
			interest(Nws.MENU_SHOW, handleShow);
			interest(Nws.CORE_ADD_GENERAL, addGeneral);
			interest(Nws.CORE_CONSCRIPTION, conscription);
			interest(Nws.ST_EXPEDITION, RecordMarchCastleId);
			interest(Nws.MARCH_TO_RETREAT, toRetreat);
			
			receive(EXE_ORDER, handleExeOrder);
			receive(VERIFY_PARAM, verifyParam);
			receive(END_CONSCRIPTION, endConscription);
		}
		
		private function toRetreat():void 
		{
			var c:CastleVO = T.db.getRecord(Cnt.VO_CASTLE, marchCastleId);
			var cs:Array = Verify.instance.getCastles(1, c);
			if (cs.length > 0)
			{
				menus[22] = [];
				for each(var id:String in cs)
				{
					c = T.db.getRecord(Cnt.VO_CASTLE,int(id));
					menus[22].push(new MenuItemVO(c.name + " " + c.genList.length, 8000 + c.id));
				}
				menus[22].push(new MenuItemVO("在野", 8000));
				send(UPDATE_ORDINARY, 22, menuDesc[22].back, menus[22], 45, 302, 3);
				Msg.instance.show(menuDesc[22].desc,75,418);
			}
			else
			{
				
			}
		}
		
		private function RecordMarchCastleId(args:Array):void 
		{
			marchCastleId = args[0];
		}
		
		private function conscription():void 
		{
			send(SELECT_GENERAL, Cnt.STATE_4, generalList);
			Msg.instance.show("增加哪位将军统领的兵士？", 80, 415);
		}
		
		private function endConscription(args:Array):void 
		{
			Gbl.activeCastle.reservist = args.shift();
			for (var i:int = 0,len:int = args.length; i < len; i++) 
			{
				generalList[i].soliders = args[i];
			}
			
			broadcast(Nws.MENU_SHOW_OR_HIDE, true);
			handleShow(50);
		}
		
		private function addGeneral(gid:int, text:String, gold:int):void
		{
			addGeneralParams = {gid: gid, gold: gold};
			send(UPDATE_ORDINARY, 23, -1, menus[23], 150, 395);
			Msg.instance.show(text, 170, 317);
		}
		
		private function handleShow(type:int):void
		{
			if (type == 0)
			{
				//隐藏菜单
				send(UPDATE_ORDINARY, 0, 0, null);
			}
			else if (type == 50)
			{
				//在策略模式中退到菜单之前，询问选择哪座城池
				broadcast(Nws.CORE_ASK);
				broadcast(Nws.INFOMATION_SWITCH_CITY, 0);
				send(UPDATE_ORDINARY, 0, 0, null);
			}
			else if (type == -50)
			{
				//在行军模式中退到菜单之前
				broadcast(Nws.MARCH_TO_SELECT_ARMY);
			}
			else if (type == -51)
			{
				//选择阵型中无返回,不处理
			}
			else if (type == 7)
			{
				send(UPDATE_ORDINARY, type, menuDesc[type].back, menus[type],140,305);
				Msg.instance.show(menuDesc[type].desc,145,410);
			}
			else
			{
				send(UPDATE_ORDINARY, type, menuDesc[type].back, menus[type]);
				Msg.instance.show(menuDesc[type].desc);
			}
		}
		
		private function handleActiveCastle(cid:int):void
		{
			handleShow(-1);
			broadcast(Nws.INFOMATION_SWITCH_CITY, 1);
			broadcast(Nws.INFOMATION_UPDATE_ORDER, 2)
			generalList.length = 0;
			for each (var id:int in Gbl.activeCastle.genList)
			{
				var g:GeneralVO = T.db.getRecord(Cnt.VO_GENERAL, id);
				generalList.push(g);
			}
		}
		
		private function clearParams():void
		{
			if (planParams.length > 0)
				planParams.length = 0;
			if(paramQueue.length > 0)
				paramQueue.length = 0;
		}
		
		private function handleExeOrder(order:int):void
		{
			if (order >= 8000)
			{
				//主将撤退前武将撤退，减去8000为城池ID
				broadcast(Nws.MA_RETREAT, order - 8000);
			}
			else if (order > 7000)
			{
				//购买装备，减去7000为装备ID
				planOrder = 5301;
				verifyCode = 0;
				var eId:int = order - 7000;
				var x:XML = T.db.getXml(Cnt.XML_EQUIP).equip.(@id == eId)[0];
				var price:int = x.@price;
				if (Gbl.activeCastle.gold >= price)
				{
					verifyParam(eId);
				}
				else
				{
					clearParams();
					handleShow(0);
					Msg.instance.show("无足够金钱购买此武器", 160, 340, 12, handleShow, [50]);
				}
			}
			else if (order > 100)
			{
				//操作指令
				if (order != 1301 && order != 2401)
					planOrder = order;
				
				(orderExecutes[order] as Function).call(null, order);
			}
			else
			{
				//转到另一个菜单
				handleShow(order);
				clearParams();
			}
			
			switch (order)
			{
				case-1: 
					broadcast(Nws.INFOMATION_SWITCH_CITY, 1);
					broadcast(Nws.STARTUP_GENERAL, false);
					break;
				case 1: 
					broadcast(Nws.INFOMATION_SWITCH_CITY, 2);
					break;
				case 4: 
					broadcast(Nws.INFOMATION_SWITCH_CITY, 3);
					break;
				case 5: 
					broadcast(Nws.INFOMATION_SWITCH_CITY, 4);
					break;
				case 5101: 
					broadcast(Nws.INFOMATION_SWITCH_CITY, 4);
					break;
			}
		}
		
		override protected function startup(... args):void
		{
			if (view == null)
			{
				//初始化视图
				view = new MenuV();
				registerView(view);
				
				//从配置文件中获取菜单列表
				menus = new Dictionary();
				menuDesc = new Dictionary();
				var xml:XML = T.db.getXml(Cnt.XML_MENU);
				for each (var x:XML in xml.menu)
				{
					var t:int = int(x.@type);
					menuDesc[t] = new MenuVO(x.@desc, x.@back);
					menus[t] = [];
					for each (var x2:XML in x.item)
					{
						menus[t].push(new MenuItemVO(x2.@show, x2.@order));
					}
				}
				menus[23] = [new MenuItemVO("是", 2301), new MenuItemVO("否", 2302)];
				menus[24] = [new MenuItemVO("是", 2401), new MenuItemVO("否", 2402)];
				
				T.db.destroyXml(Cnt.XML_MENU);
				
				registerOrderExecute();
				
				//配置动态的城市功能菜单1-武器 2-学问馆 3-医馆 4-商行
				functions = new Dictionary();
				equips1 = new Dictionary();
				equips2 = new Dictionary();
				var cxml:XML = T.db.getXml(Cnt.XML_CASTLE);
				for each (var c:XML in cxml.castle)
				{
					var cid:int = int(c.@id);
					functions[cid] = String(c.@funList).split(',');
					if ((functions[cid] as Array).indexOf("1") != -1)
					{
						equips1[cid] = String(c.eq1).split(',');
						equips2[cid] = String(c.eq2).split(',');
					}
				}
				
				menuDesc[-3] = new MenuVO("此处是市集,往何处去?", -1);
				functionEnum = [];
				functionEnum.push(new MenuItemVO("png.menu.m1", 5301));
				functionEnum.push(new MenuItemVO("png.menu.m2", 5302));
				functionEnum.push(new MenuItemVO("png.menu.m3", 5303));
				functionEnum.push(new MenuItemVO("png.menu.m4", 11));
				
				menuDesc[20] = new MenuVO("购买何种?", 10);
				menuDesc[21] = new MenuVO("撤退到哪座城?", -50);
				menuDesc[22] = new MenuVO("撤退到哪座城?", -51);
				
				handlePool = [];
				for (var i:int = 0; i < 3; i++)
				{
					handlePool.push(new HandleObject());
				}
				
				paramQueue = [];
				planParams = [];
				generalList = [];
			}
		}
		
		private function registerOrderExecute():void
		{
			orderExecutes = new Dictionary();
			orderExecutes[101] = exe_1;
			orderExecutes[102] = exe_1;
			orderExecutes[103] = exe_1;
			orderExecutes[104] = exe_1;
			orderExecutes[201] = exe_2;
			orderExecutes[202] = exe_2;
			orderExecutes[203] = exe_2;
			orderExecutes[301] = exe_3;
			orderExecutes[302] = exe_3;
			orderExecutes[303] = exe_3;
			orderExecutes[401] = exe_4;
			orderExecutes[402] = exe_4;
			orderExecutes[403] = exe_4;
			orderExecutes[404] = exe_4;
			orderExecutes[405] = exe_4;
			orderExecutes[406] = exe_4;
			orderExecutes[501] = exe_5;
			orderExecutes[601] = exe_6;
			orderExecutes[602] = exe_6;
			orderExecutes[701] = exe_7;
			orderExecutes[702] = exe_7;
			orderExecutes[703] = exe_7;
			orderExecutes[704] = exe_7;
			orderExecutes[705] = exe_7;
			orderExecutes[801] = exe_8;
			orderExecutes[802] = exe_8;
			orderExecutes[803] = exe_8;
			orderExecutes[804] = exe_8;
			orderExecutes[805] = exe_8;
			orderExecutes[806] = exe_8;
			orderExecutes[901] = exe_9;
			orderExecutes[902] = exe_9;
			orderExecutes[1001] = exe_10;
			orderExecutes[1002] = exe_10;
			orderExecutes[1003] = exe_10;
			orderExecutes[1004] = exe_10;
			orderExecutes[1101] = exe_11;
			orderExecutes[1201] = exe_12;
			orderExecutes[1202] = exe_12;
			orderExecutes[1301] = exe_13;
			orderExecutes[1302] = exe_13;
			orderExecutes[1401] = exe_14;
			orderExecutes[1402] = exe_14;
			orderExecutes[1501] = exe_15;
			orderExecutes[1502] = exe_15;
			orderExecutes[2301] = exe_23;
			orderExecutes[2302] = exe_23;
			orderExecutes[2401] = exe_24;
			orderExecutes[2402] = exe_24;
			orderExecutes[5101] = exe_51;
			orderExecutes[5201] = exe_52;
			orderExecutes[5202] = exe_52;
			orderExecutes[5203] = exe_52;
			orderExecutes[5204] = exe_52;
			orderExecutes[5301] = exe_53;
			orderExecutes[5302] = exe_53;
			orderExecutes[5303] = exe_53;
		
		}
		
		private function exe_1(order:int):void
		{
			switch (order)
			{
				case 101: 
					paramQueue.push(handlePool[0].setHandle(selectCastle, 1, "移动至何处？\n请指定"));
					paramQueue.push(handlePool[1].setHandle(selectGeneral, -1, generalList ,Cnt.STATE_2));
				break;
				case 102: 
					paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, generalList));
				break;
				case 103: 
					paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, generalList));
					paramQueue.push(handlePool[1].setHandle(enquire, 2));
				break;
				case 104:
					handleShow(0);
					Msg.instance.show("是否将现况记入史册?", 160, 340, 12, handleShow, [13], handleShow, [1]);
					return;
				break;
			}
			nextParam();
		}
		
		private function exe_2(order:int):void
		{
			switch (order)
			{
				case 201: 
					paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, generalList));
					paramQueue.push(handlePool[1].setHandle(enquire, 2));
				break;
				case 202: 
					paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, generalList));
					paramQueue.push(handlePool[1].setHandle(enquire, 2));
				break;
				case 203: 
					paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, generalList));
					paramQueue.push(handlePool[1].setHandle(enquire, 2));
				break;
			}
			nextParam();
		}
		
		private function exe_3(order:int):void
		{
			switch (order)
			{
				case 301:
					handleShow(0);
					Msg.instance.show("同盟功能暂未开放，敬请期待", 140, 360, 12, handleShow, [3]);
					return;
					break;
				case 302:
					paramQueue.push(handlePool[0].setHandle(selectCastle, 4, "离间哪个城内的武将？\n请指定"));
					paramQueue.push(handlePool[1].setHandle(selectGeneral, 5, null,Cnt.STATE_1,"请指定离间哪位武将"));
					paramQueue.push(handlePool[2].setHandle(selectGeneral, -1, generalList));
					break;
				case 303:
					paramQueue.push(handlePool[0].setHandle(selectCastle, 4, "拉拢哪个城内的武将？\n请指定"));
					paramQueue.push(handlePool[1].setHandle(selectGeneral, 5, null,Cnt.STATE_1,"请指定拉拢哪位武将"));
					paramQueue.push(handlePool[2].setHandle(selectGeneral, -1, generalList));
					break;
			}
			nextParam();
		}
		
		private function exe_4(order:int):void
		{
			switch (order)
			{
				case 401:
					//出征
					isNeedRequire = true;
					var d2:Array = [
					{ label:"金", max:Gbl.activeCastle.gold, len:4, minLen:1 },
					{ label:"米", max:Gbl.activeCastle.rice, len:4, minLen:1}];
					paramQueue.push(handlePool[0].setHandle(selectCastle, 7, "进攻哪座城？"));
					paramQueue.push(handlePool[1].setHandle(selectGeneral, -1, generalList,Cnt.STATE_3));
					paramQueue.push(handlePool[2].setHandle(selectNumber, 0, "随军之金米数目", d2, 4));
					nextParam();
					break;
				case 402:
					//征兵
					var m:int = Gbl.activeCastle.gold * 5;
					var d:Array = [ { label:"士兵", max:m,len:5,minLen:3}];
					paramQueue.push(handlePool[0].setHandle(selectNumber, 0, "每 100士兵需 20两金", d, 4,210));
					nextParam();
					break;
				case 403:
					handleShow(13);//侦察
					break;
				case 404:
					if (Gbl.activeCastle.captainID == Gbl.activePlayer.monarchID)
					{
						handleShow(0);
						Msg.instance.show("君主所在城池\n太守由君主担任", 160, 332, 12, handleShow, [4]);
					}
					else
					{
						//任命太守
						paramQueue.push(handlePool[1].setHandle(selectGeneral, -1, generalList,Cnt.STATE_1, "任命何人为太守？"));
						nextParam();
					}
					break;
				case 405: 
					broadcast(Nws.STRATEGY_SWITCH_MAP);//切换地图
					break;
				case 406: 
					broadcast(Nws.GENERAL_OPEN, generalList, Cnt.MODE_STRATEGY);///查看武将资料
					break;
			}
		}
		
		private function exe_5(order:int):void
		{
			var d:Array = [ 
			{ label:"金", max:Gbl.activeCastle.gold, len:4, minLen:1 },
			{label:"米", max:Gbl.activeCastle.rice, len:4, minLen:1 },
			{label:"宝",max:Gbl.activeCastle.treasure,len:2,minLen:1}];
			paramQueue.push(handlePool[0].setHandle(selectCastle, 6, "运送到哪座城？\n请指定"));
			paramQueue.push(handlePool[1].setHandle(selectNumber, 0, "运送数目",d,5));
			nextParam();
		}
		
		private function exe_6(order:int):void
		{
			//赏赐民众
			var d:Array = [ 
			{ label:"金", max:Gbl.activeCastle.gold ,len:3,minLen:2},
			{ label:"米", max:Gbl.activeCastle.rice ,len:3,minLen:2} ];
			paramQueue.push(handlePool[0].setHandle(selectNumber, 0, "赏赐民众数目？", d, 6));
			nextParam();
		}
		
		private function exe_7(order:int):void
		{
			switch (order)
			{
				case 701:
					handleShow(0);
					broadcast(Nws.MA_MOVE);//移动
					break;
				case 702:
					handleShow(0);
					broadcast(Nws.MA_ATTACK);//攻击
					break;
				case 703:
					handleShow(0);
					broadcast(Nws.MA_USE_PLOT);//使用计谋
					break;
				case 704:
					broadcast(Nws.MA_ENCAMP);//待机
					break;
				case 705:
					//撤退
					var c:CastleVO = T.db.getRecord(Cnt.VO_CASTLE, marchCastleId);
					var cs:Array = Verify.instance.getCastles(1, c);
					if (cs.length > 0)
					{
						menus[21] = [];
						for each(var id:String in cs)
						{
							c = T.db.getRecord(Cnt.VO_CASTLE,int(id));
							menus[21].push(new MenuItemVO(c.name + " " + c.genList.length, 8000 + c.id));
						}
						send(UPDATE_ORDINARY, 21, menuDesc[21].back, menus[21], 95, 332, 3);
						Msg.instance.show(menuDesc[21].desc,125,308);
					}
					else
					{
						//没有城池可退
					}
					break;
			}
		}
		
		private function exe_8(order:int):void
		{
			switch (order)
			{
				case 801:
					
					break;
				case 802:
					
					break;
				case 803:
					
					break;
				case 804:
					
					break;
				case 805:
					
					break;
				case 806:
					
					break;
			
			}
		}
		
		private function exe_9(order:int):void
		{
			switch (order)
			{
				case 901:
					
					break;
				case 902:
					
					break;
			}
		}
		
		private function exe_10(order:int):void
		{
			var start:int;
			var end:int;
			
			//购买剑、刀、矛、防具
			switch (order)
			{
				case 1001: 
					start = 0;
					end = 4;
					break;
				case 1002: 
					start = 4;
					end = 8;
					break;
				case 1003: 
					start = 8;
					end = 12;
					break;
				case 1004: 
					start = 12;
					end = 16;
					break;
			}
			
			var d:int;
			var m:Array = [];
			var xml:XML = T.db.getXml(Cnt.XML_EQUIP);
			var es:Array = equips1[Gbl.activeCastle.id] as Array;
			for (var i:int = start; i < end; i++)
			{
				d = 7000 + int(es[i]);
				var x:XML = xml.equip.(@id == es[i])[0];
				m.push(new MenuItemVO(x.@name + " " + x.@price, d));
			}
			send(UPDATE_ORDINARY, 20, menuDesc[20].back, m, 105);
			Msg.instance.show(menuDesc[20].desc);
		}
		
		private function exe_11(order:int):void
		{
			if (Gbl.activeCastle.treasure > 0)
			{
				var d:Array = [ { label:"宝物", max:Gbl.activeCastle.treasure,len:2,minLen:1 } ];
				paramQueue.push(handlePool[0].setHandle(selectNumber, 0, "每件宝物值 100两金", d, 11,220));
				nextParam();
			}
			else
			{
				handleShow(0);
				Msg.instance.show("现今此城内并无宝物\n请下达其他命令", 140, 332, 12, handleShow, [11]);
			}
		}
		
		private function exe_12(order:int):void
		{
			var x:XML = T.db.getXml(Cnt.XML_CASTLE).castle.(@id==Gbl.activeCastle.id)[0];
			var d:Array;
			var m:int;
			switch (order)
			{
				case 1201:
					m = Gbl.activeCastle.gold * Number(x.@buy) * 0.01;
					d = [{label:"米",max:m,len:4,minLen:1}];
					paramQueue.push(handlePool[0].setHandle(selectNumber, 0, "每100两金可得 "+x.@buy+"石米", d, 12,220));
					break;
				case 1202:
					d = [ { label:"米", max:Gbl.activeCastle.rice,len:4,minLen:1 } ];
					paramQueue.push(handlePool[0].setHandle(selectNumber, 0, "每100石米可卖得 "+x.@sell+"两金", d, 12,220));
					break;
			}
			nextParam();
		}
		
		private function exe_13(order:int):void
		{
			//是否消耗军令
			switch (order)
			{
				case 1301: 
					//Gbl.activePlayer.order--;//军令暂时屏蔽
					broadcast(Nws.INFOMATION_SWITCH_CITY, 0);
					handleShow(0);
					Kyb.setOperater(null);
					
					if (planOrder == 403)
						incidentCallBack();
					else
						broadcast(Nws.REPORT_PLAY_INCIDENT, planOrder, incidentCallBack); //播放动画****暂时只是图片
					break;
				case 1302: 
					handleExeOrder( -1); //取消执行指令
					planParams.length = 0;
					break;
			}
		}
		
		private function incidentCallBack():void
		{
			//broadcast(Nws.INFOMATION_UPDATE_ORDER, 1);//更新军令,暂时屏蔽
			broadcast(planOrder.toString(), planParams); //确定执行指令
			planParams.length = 0;
			
			if (planOrder == 402)
			{
				broadcast(Nws.REPORT_PLAY_INCIDENT,0);
			}
		}
		
		private function exe_14(order:int):void
		{
			switch (order) 
			{
				case 1401:
					var d:Array = [{label:"金",max:Gbl.activeCastle.gold,len:3,minLen:2}];
					paramQueue.push(handlePool[0].setHandle(selectNumber, 0, "赏赐几两金？", d, 14,220));
					paramQueue.push(handlePool[1].setHandle(selectGeneral, 5, generalList,Cnt.STATE_1, "赏赐哪位武将？"));
					nextParam();
				break;
				case 1402:
					if (Gbl.activeCastle.treasure > 0)
					{
						paramQueue.push(handlePool[0].setHandle(selectGeneral, 5, generalList,Cnt.STATE_1, "赏赐哪位武将？"));
						nextParam();
					}
					else
					{
						handleShow(0);
						Msg.instance.show("现今此城内并无宝物\n请下达其他命令", 140, 332, 12, handleShow, [14]);
					}
				break;
			}
		}
		
		private function exe_15(order:int):void
		{
			switch (order) 
			{
				case 1501:
					handleShow(0);
					broadcast(Nws.ST_RECONNOITER);
				break;
				case 1502:
					handleExeOrder(50); //取消执行指令
				break;
			}
		}
		
		private function exe_23(order:int):void
		{
			//询问是否给金收为部下
			switch (order)
			{
				case 2301: 
					var gid:int = addGeneralParams.gid;
					var gold:int = addGeneralParams.gold;
					if (gold == -1)
					{
						//特殊武将
						var x:XML = T.db.getXml(Cnt.XML_ALGORITHM).special.item.(@id == gid)[0];
						var rate:Number = x.@rate;
						if (Gbl.activePlayer.id != int(x.@playerID))
						{
							rate = 1 - rate;
						}
						if (Math.random() < rate)
						{
							gold = 0;
						}
						else
						{
							broadcast(Nws.GENERAL_SHOW_HEAD, gid);
							handleShow(0);
							Msg.instance.show("吾无意侍奉一般人\n汝今日之心意还请收回", 160, 332, 12, handleExeOrder, [50]);
							return;
						}
					}
					if (Gbl.activeCastle.gold >= gold)
					{
						Gbl.activeCastle.storeList.shift();
						Gbl.activeCastle.genList.push(String(gid));
						Gbl.activeCastle.gold -= gold;
						broadcast(Nws.GENERAL_SHOW_HEAD, gid);
						handleShow(0);
						Msg.instance.show("愿效犬马之劳", 160, 332, 12, handleExeOrder, [50]);
					}
					else
					{
						Msg.instance.show("如今些城内并无足够金钱\n请下达其他命令", 160, 332, 12, handleExeOrder, [50]);
					}
					break;
				case 2302: 
					handleShow( -1); //取消执行指令
					break;
			}
		}
		
		private function exe_24(order:int):void
		{
			//出征前询问
			switch (order) 
			{
				case 2401:
					//没君主且武将大于1人则要选主将
					var gs:Array = planParams[1];
					if (gs.length > 1 && gs.indexOf(String(Gbl.activePlayer.monarchID)) == -1)
					{
						var t:Array = [];
						for each(var gid:String in gs)
						{
							t.push(T.db.getRecord(Cnt.VO_GENERAL, int(gid)));
						}
						paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, t,Cnt.STATE_5, "请任命主将"));
						nextParam();
					}
					else
					{
						//第一个参数必定是君主或仅有的唯一武将
						planParams.push(int(gs[0]));
						handleShow(13);
					}
				break;
				case 2402:
					handleShow(4); //取消执行指令
					clearParams();
				break;
			}
		}
		
		private function exe_51(order:int):void
		{
			//只有5101一条指令，根据城池配置生成功能菜单
			if (curCastle != Gbl.activeCastle)
			{
				curCastle = Gbl.activeCastle;
				var fs:Array = functions[Gbl.activeCastle.id];
				var t:Array = [];
				for each (var i:int in fs)
				{
					t.push(functionEnum[i - 1]);
				}
				menus[-3] = t;
			}
			send(UPDATE_ORDINARY, -3, menuDesc[-3].back, menus[-3], 150);
			Msg.instance.show(menuDesc[-3].desc, 125, 340)
		}
		
		private function exe_52(order:int):void
		{
			switch (order)
			{
				case 5201:
					broadcast(Nws.FIGHT_SELECT_FORMATION,1);
					break;
				case 5202:
					broadcast(Nws.FIGHT_SELECT_FORMATION,2);
					break;
				case 5203:
					broadcast(Nws.FIGHT_SELECT_FORMATION,3);
					break;
				case 5204:
					broadcast(Nws.FIGHT_SELECT_FORMATION,4);
					break;
			}
		}
		
		private function exe_53(order:int):void
		{
			switch (order)
			{
				case 5301:
					//购买兵器
					paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, generalList,Cnt.STATE_1, "何人购买装备？"));
					paramQueue.push(handlePool[1].setHandle(handleShow, 10));
					nextParam();
					break;
				case 5302:
					//读书
					paramQueue.push(handlePool[0].setHandle(selectGeneral, 3, generalList));
					paramQueue.push(handlePool[1].setHandle(enquire, 2));
					nextParam();
					break;
				case 5303:
					//治疗
					var t:Array = [];
					for each(var g:GeneralVO in generalList)
					{
						if (g.strength < g.strengthUp)
							t.push(g);
					}
					if (t.length > 0)
					{
						paramQueue.push(handlePool[0].setHandle(selectGeneral, -1, t,Cnt.STATE_1,"让何人去医馆？"));
						paramQueue.push(handlePool[1].setHandle(enquire, 2));
						nextParam();
					}
					else
					{
						handleShow(0);
						Msg.instance.show("些城内并无受伤武将\n请下达其他命令",140,330,12,handleShow,[-3]);
					}
					break;
			}
			
		}
		
		private function selectGeneral(verify:int, gs:Array, state:int = 1, msg:String = "派谁前往？请指定"):void
		{
			verifyCode = verify;
			Msg.instance.show(msg, 80, 415);
			if (gs == null)
			{
				//如果gs为null，则上一个选择的参数为城池
				gs = [];
				var cid:int = planParams[planParams.length - 1];
				var c:CastleVO = T.db.getRecord(Cnt.VO_CASTLE, cid);
				for each(var gid:String in c.genList)
				{
					gs.push(T.db.getRecord(Cnt.VO_GENERAL, int(gid)));
				}
			}
			send(SELECT_GENERAL, state, gs);
			broadcast(Nws.MENU_SHOW_OR_HIDE, false);
		}
		
		private function selectCastle(verify:int, msg:String):void
		{
			verifyCode = verify;
			var cs:Array = Verify.instance.getCastles(verifyCode,Gbl.activeCastle);
			if (cs.length > 0)
			{
				Msg.instance.show(msg, 190, 332);
				handleShow(0);
				broadcast(Nws.INFOMATION_SWITCH_CITY, 0);
				broadcast(Nws.STRATEGY_REQUEST_CASTLE, 2, cs);
			}
			else
			{
				handleShow(0);
				Msg.instance.show("没有可选的城池", 170, 332, 12, handleShow, [ -1]);
				clearParams();
			}
		}
		
		/**
		 * @param data [{label:"",max:0,unit:null},...]
		 */
		private function selectNumber(verify:int, msg:String,data:Array,backMenu:int,tx:int = 160,ty:int = 370):void
		{
			verifyCode = verify;
			Msg.instance.show(msg, 160, 320);
			
			if(planOrder != 402)
				broadcast(Nws.INFOMATION_SWITCH_CITY, 4);
				
			send(SELECT_NUMBER, data,backMenu, tx, ty);
		}
		
		private function enquire(verify:int):void
		{
			verifyCode = verify;
			var xml:XML = T.db.getXml(Cnt.XML_ALGORITHM).enquire.item.(@id == String(planOrder))[0];
			var g:GeneralVO;
			var x:XML
			var v:int;
			
			if (planOrder == 103 || planOrder == 201 || planOrder == 202 || planOrder == 203)
			{
				//防灾、土地开垦、鼓励工商、开发市集
				g = T.db.getRecord(Cnt.VO_GENERAL, planParams[0]);
				x = xml.cost.(@minKno <= g.knowledge && g.knowledge <= @maxKno)[0];
				var vs:Array = String(x.@values).split(',');
				var r:int = Math.random() * vs.length;
				v = vs[r];
			}
			else if (planOrder == 402 || planOrder == 5303)
			{
				//征兵、治疗
				v = xml.@cost;
			}
			else if (planOrder == 5302)
			{
				//读书
				g = T.db.getRecord(Cnt.VO_GENERAL, planParams[0]);
				x = xml.cost.(@minKno <= g.knowledge && g.knowledge <= @maxKno)[0];
				v = x.@value;
			}
			
			handleShow(0);
			var s:String = String(xml.@text).replace("%%",v);
			Msg.instance.show(s, 160, 320, 12, verifyParam, [v]);
		}
		
		/**
		 * 校验参数
		 * @param	order 指令
		 * @param	param 参数
		 */
		private function verifyParam(param:*):void
		{
			if (verifyCode > 0 && Verify.instance.test(verifyCode, param) == false)
			{
				if (verifyCode == 2)
				{
					Msg.instance.show("如今些城内并无足够金钱\n请下达其他命令", 160, 332, 12, handleExeOrder, [50]);
				}
				else if (verifyCode == 3)
				{
					handleShow(0);
					broadcast(Nws.MENU_SHOW_OR_HIDE, true);
					broadcast(Nws.GENERAL_SHOW_HEAD, param);
					broadcast(Nws.INFOMATION_UPDATE_ORDER, 3);
					Msg.instance.show("臣不需如此", 160, 340, 12, stopStudy);
				}
			}
			else
			{
				if (verifyCode == -1 || verifyCode == 3 || verifyCode == 5)
					broadcast(Nws.MENU_SHOW_OR_HIDE, true);
					
				planParams.push(param);
				nextParam();
			}
		}
		
		private function stopStudy():void
		{
			handleExeOrder( -3);
			broadcast(Nws.STARTUP_GENERAL, false);
			broadcast(Nws.INFOMATION_UPDATE_ORDER, 2);
		}
		
		private function nextParam():void
		{
			if (paramQueue.length > 0)
			{
				var h:HandleObject = paramQueue.shift();
				h.execute();
			}
			else
			{
				if (planOrder == 404)
				{
					broadcast(Nws.INFOMATION_SWITCH_CITY, 0);
					handleShow(0);
					//担任太守
					broadcast(Nws.ST_APPOINT, planParams);
					planParams.length = 0;
				}
				else if (planOrder == 401 && isNeedRequire)
				{
					//出征前询问
					var gs:Array = planParams[1];
					var n:int;
					for each(var gid:String in gs)
					{
						var g:GeneralVO = T.db.getRecord(Cnt.VO_GENERAL, int(gid));
						n += g.soliders;
					}
					var s:String = "兵力 " + n;
					s += "  金 " + planParams[2][0] + "  米 " + planParams[2][1];
					s += "\n可否出征?";
					Msg.instance.show(s, 160,330);
					send(UPDATE_ORDINARY, 24, 4, menus[24], 150, 380);
					
					isNeedRequire = false;
				}
				else
				{
					//询问是否执行军令
					handleShow(13);
				}
			}
		}
		
		/**菜单数据*/
		private var menus:Dictionary;
		/**菜单描述*/
		private var menuDesc:Dictionary;
		/**城市功能列表*/
		private var functions:Dictionary;
		/**装备列表1城镇 2村庄*/
		private var equips1:Dictionary;
		private var equips2:Dictionary;
		/**城市功能枚举*/
		private var functionEnum:Array;
		/**指令处理方法集*/
		private var orderExecutes:Dictionary;
		/**选择参数的执行序列*/
		private var paramQueue:Array;
		/**选择参数处理方法池*/
		private var handlePool:Array;
		/**当前在执行的命令*/
		private var planOrder:int;
		/**准备执行的指令的参数列表*/
		private var planParams:Array;
		/**当前城池的武将列表*/
		private var generalList:Array;
		/**参数检验代号，为0则不需要检验*/
		private var verifyCode:int;
		/**当前城池*/
		private var curCastle:CastleVO;
		/**搜索到武将后添加时需要的参数*/
		private var addGeneralParams:Object;
		/**是否需要询问出征前的情况*/
		private var isNeedRequire:Boolean;
		/**用于记录行军战场的城池ID*/
		private var marchCastleId:int;
		
		/**更新菜单项，type为0时隐藏菜单*/
		static public const UPDATE_ORDINARY:String = "update_ordinary";
		/**执行指令*/
		static public const EXE_ORDER:String = "exe_order";
		/**添加指令参数*/
		static public const VERIFY_PARAM:String = "verify_param";
		/**选择武将*/
		static public const SELECT_GENERAL:String = "select_general";
		/**选择敌方武将*/
		static public const SELECT_GENERAL_ENEMY:String = "select_general_enemy";
		/**选择武器*/
		static public const SELECT_EQUIP:String = "select_equip";
		/**选择金米宝兵,[{label:"",max:0,unit:null},...],tx,ty*/
		static public const SELECT_NUMBER:String = "select_number";
		/**选择君主*/
		static public const SELECT_MONARCH:String = "select_monarch";
		/**结束征兵*/
		static public const END_CONSCRIPTION:String = "end_conscription";
		/***/
		//static public const CLEAR_PARAMS:String = "clear_params";
	
	}

}

class HandleObject
{
	public function setHandle(fun:Function, ... rest):HandleObject
	{
		handle = fun;
		params = rest;
		return this;
	}
	
	public function execute():void
	{
		handle.apply(null, params);
	}
	
	private var handle:Function;
	private var params:Array;
}